// @ 516
// _(:з」∠)_
// 模拟属性操作
// simulate attribute operations

// 判断元素是否有某个class
Element.prototype.hasClass = function(cls) {
    return (new RegExp('(\\s|^)' + cls + '(\\s|$)')).test(this.classList);
}

// 元素添加class
Element.prototype.addClass = function(cls) {
    if ( !this.hasClass(cls) ) {
        this.classList.add(cls);
    }
    return this;
}

// 添加所有class
Element.prototype.addAllClass = function(cls) {
    var item = this.parentNode.children;
    for ( var x = 0; x < item.length; x++ ) {
        if ( !item[x].hasClass(cls) ) {
            item[x].classList.add(cls);
        }
    }
    return this;
}

// 元素移除class
Element.prototype.removeClass = function(cls) {
    if ( this.hasClass(cls) ) {
        this.classList.remove(cls);
    }
    return this;
}

// 移除所有class
Element.prototype.removeAllClass = function(cls) {
    var item = this.parentNode.children;
    for ( var x = 0; x < item.length; x++ ) {
        if ( item[x].hasClass(cls) ) {
            item[x].classList.remove(cls);
        }
    }
    return this;
}

function removeAllClass( cls, item ) {
    for ( var x = 0; x < item.length; x++ ) {
        if ( item[x].hasClass(cls) ) {
            item[x].classList.remove(cls);
        }
    }
}

// 切换元素class
Element.prototype.toggleClass = function(cls) {
    if ( this.hasClass(cls) ) {
        this.removeClass(cls);
    } else {
        this.addClass(cls);
    }
    return this;
}

// 查找元素相邻节点
Element.prototype.siblings = function(cls) {
    let nodeArry = [],
        parentChild = "";

    if ( cls != null ) {
        if ( cls.indexOf(".") > -1 ) {
            parentChild = this.parentNode.querySelectorAll(cls);
        } else {
            parentChild = this.parentNode.querySelectorAll(this.tagName);
        }
    } else {
        parentChild = this.parentNode.querySelectorAll(this.tagName);
    }
    
    let parentChildLent = parentChild.length;
        
    for(let x = 0; x < parentChildLent; x++){
        if( parentChild[x] != this ){
            nodeArry.push(parentChild[x]);
        }
    }

    return nodeArry;
}

// 清空内容
Element.prototype.empty = function() {
    let child = this.children;

    for ( let x = child.length - 1; x >= 0; x-- ) {
        child[x].remove();
    }

    return this;
}

// 显示元素
Element.prototype.show = function() {
    this.style.display = "block";
    return this;
}

// 显示所有元素
function showAllElmt( arry ) {
    for ( var x = 0, len = arry.length; x < len; x++ ) {
        arry[x].style.display = "block";
    }
}

// 隐藏元素
Element.prototype.hide = function( arry ) {
    this.style.display = "none";
    return this;
}

// 隐藏所有元素
function hideAllElmt( arry ) {
    for ( var x = 0, len = arry.length; x < len; x++ ) {
        arry[x].style.display = "none";
    }
}

// 元素当前的偏移
Element.prototype.offset = function() {
    var pos = { left: 0, top: 0 };

    pos.left += this.offsetLeft;
    pos.top += this.offsetTop;
    
    return pos;
}

 // 判断元素是否是当前项子级
 Element.prototype.isChildren = function( parent ) {
    var childrenx = [];

    function isNextChildren( parent, children, childrenx ) {
        for ( let x = 0; x < children.length; x++ ) {
            let thisChild = children[x].children;
            childrenx.push( children[x] );
            if ( thisChild.length > 0 ) {
                isNextChildren(  parent, thisChild, childrenx  );
            } else {
                continue;
            }
        }
    }
    
    isNextChildren(  parent, parent.children, childrenx  );

    for ( let x = 0; x < childrenx.length; x++ ) {
        if ( childrenx[x] == this ) {
            return true;
        } else if ( x == childrenx.length - 1 ) {
            return false;
        }
    }
}

// 获取当前元素索引
Element.prototype.currentIndex = function() {
    if ( this ) {
        var parent = this.parentNode,
            elmtList = parent.querySelectorAll( this.nodeName ),
            listToArray = Array.from( elmtList );

        return listToArray.indexOf( this );
    } else {
        throw "元素不存在";
    }
}